<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace backend\components\filesystem;


use creocoder\flysystem\Filesystem;
use EQingdan\Flysystem\Qiniu\QiniuAdapter;
use League\Flysystem\AdapterInterface;
use yii\base\InvalidConfigException;

class QiniuFilesystem extends Filesystem
{

    public $accessKey;
    public $secretKey;
    public $bucket;
    public $domain;


    /**
     * @inheritdoc
     */
    public function init()
    {
        if ($this->accessKey === null) {
            throw new InvalidConfigException('The "accessKey" property must be set.');
        }

        if ($this->secretKey === null) {
            throw new InvalidConfigException('The "secretKey" property must be set.');
        }

        if ($this->bucket === null) {
            throw new InvalidConfigException('The "bucket" property must be set.');
        }

        parent::init();
    }

    /**
     * @return AdapterInterface
     */
    protected function prepareAdapter()
    {
        return new QiniuAdapter($this->accessKey, $this->secretKey, $this->bucket, $this->domain);
    }
}